package com.zoranjoka.chordis.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.zoranjoka.chordis.dao.AlbumDAO;
import com.zoranjoka.chordis.dao.ArtistDAO;
import com.zoranjoka.chordis.entities.Album;
import com.zoranjoka.chordis.entities.AlbumBean;
import com.zoranjoka.chordis.entities.Artist;
import com.zoranjoka.chordis.entities.ArtistBean;

/**
 * Start page of application chordis.
 */
public class Index {
	@Inject
	private ArtistDAO dataArtist;
	@Inject
	private AlbumDAO dataAlbum;

	@Property
	private List<Artist> artists = new ArrayList<Artist>();
	@Property
	private List<Album> albums = new ArrayList<Album>();
	@Property
	@Persist
	private com.zoranjoka.chordis.entities.Artist artist; 
	@Property
	@Persist
	private com.zoranjoka.chordis.entities.Album album1;
	@Property
	@Persist
	private com.zoranjoka.chordis.entities.Album album2;

	void onActivate() {
		// long i = 3;
		// artist = dataArtist.findById(i = 5);
		System.out.println("onActivate fired...");
		artist = new ArtistBean();
		album1 = new AlbumBean();
		album2  = new AlbumBean();
		artists = dataArtist.getAll();
		albums = dataAlbum.getAll();
	}

	@OnEvent(component = "fill")
	@CommitAfter
	void onFillShit() {
		System.out.println("onFill fired...");
		artist.setName("zoltan");

		album1.setName("prvi album");
		album1.setYear(2001);

		album2.setName("drugi album");
		album2.setYear(2012);
		artist.addAlbum(album1);
		artist.addAlbum(album2);
		album1.setArtist(artist);
		album2.setArtist(artist);

		dataArtist.save(artist);
		
	}

	@OnEvent(component = "update")
	@CommitAfter
	void onUpdate() {
		 long i = 48;
		 artist = dataArtist.findById(i);
		artist.setName("djole");
		album1 = artist.getAlbums().get(0);
		album1.setName("test2album");
		album1.setYear(23);
		artist.deleteAlbum(album1);
		dataArtist.update(artist);
	}

	@OnEvent(component = "delete")
	@CommitAfter
	void onDelete() {
		artist.deleteAlbum(album1);
		dataArtist.update(artist);
	}
}
